/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.LibreGeoSocial.Api.Note;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.LibreGeoSocial.App.Utils.LocationUtils;
import com.google.android.maps.MapView;

public class NoteCard extends GenericCard {

	
	private ImageView imageUser;
	private TextView lbTitle, lbBody, lbDate, lbDist, lbUploader;
	private ScrollView lbscroll;
	
	private MapView mapView;
	
	private Double latitude, longitude;
	private Note mNote;
	
	private static final int MENU_SHARE = Menu.FIRST + 10;


    public void onCreate(Bundle icicle) {

    	super.onCreate(icicle);
        
        setContentView(R.layout.generic_card); 
        
        mNote = (Note) mNode;
        
        imageUser = (ImageView)this.findViewById(R.id.img);
        imageUser.setImageResource(R.drawable.note_100); 

        lbscroll = (ScrollView) this.findViewById(R.id.ScrollView01);
        lbTitle = (TextView) this.findViewById(R.id.lbTitle);
        lbBody = (TextView) this.findViewById(R.id.lbBody);   
        lbDate = (TextView) this.findViewById(R.id.lbDate);
        lbDist = (TextView) this.findViewById(R.id.lbDist); 
        lbUploader = (TextView) this.findViewById(R.id.lbUploader);

        
        Intent i = getIntent();

        lbTitle.setText (mNote.getTitle());
        lbBody.setText  (mNote.getBody());
        lbDate.setText  ("Date: " + mNote.getSince().split(" ")[0]); 
        
        // Calculate distance
        float dist = LocationUtils.calculateDistance (LocationService.getCurrentLocation(),
       		 	mNote.getLocation());
        
        lbDist.setText  ("Distance: " + LocationUtils.displayDistance(dist, LocationUtils.AUTO));

        setIconId(R.drawable.comment_38);
        showMap(mNote.getLongitude(), mNote.getLatitude());  

    }
    
    @Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, MENU_SHARE, 0, "Share")
                .setAlphabeticShortcut('B')
				.setIcon(R.drawable.up);

		        
        return true;
	}
	
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {

    	switch (item.getItemId()) {
    	
    		case MENU_SHARE:

    			String tmp_path = getResources().getString(R.string.tmp_path);

                //create the dir if not exists
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra(Intent.EXTRA_TEXT, mNote.getBody());
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_SUBJECT, "[LGS-Note] " + mNote.getTitle());
                startActivity(i);


    	}

    	return super.onOptionsItemSelected(item);
    }
	
}

